package com.yicong.ants.view;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yicong.ants.R;
import com.yicong.ants.utils.KeyBoardUtils;

/**
 * 发送消息 popupWindow
 */
public class SendMsgPop extends PopupWindow {

    private View rootView;
    private Activity mContext;
    private SendMsgPopCall callBack;
    private EditText editText;

    public SendMsgPop(Activity activity) {
        super(activity);
        mContext = activity;
        if (activity instanceof SendMsgPopCall) {
            callBack = (SendMsgPopCall) activity;
        }
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        this.setBackgroundDrawable(dw);
        rootView = LayoutInflater.from(activity).inflate(R.layout.popup_window_send_msg, null);
        editText = rootView.findViewById(R.id.ed_data);
        TextView btnMsg = rootView.findViewById(R.id.tv_send);
        btnMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != callBack) {
                    callBack.callBack(editText.getText().toString());
                }
            }
        });
        setContentView(rootView);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        KeyBoardUtils.closeKeybord(editText, mContext);
    }

    public void showAtLocation(View parent, int gravity, int x, int y) {
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); // 在显示输入法之后调用，否则popupwindow会在窗口底层
        super.showAtLocation(parent, gravity, x, y);
    }

    public interface SendMsgPopCall {
        void callBack(String msg);
    }
}
